Repository navigation
fix(openai): count base64 embedding vector dimensions - #2077
as992949791 wants to merge 3 commits into
Conversation
Generated with OpenAI Codex.
Generated with OpenAI Codex.
|
|
| if isinstance(embedding_vector, str): | ||
| try: | ||
| # Base64 embeddings contain one float32 per dimension. | ||
| dimensions = len(base64.b64decode(embedding_vector)) // 4 |
There was a problem hiding this comment.
Malformed strings bypass the fallback
base64.b64decode silently discards invalid characters by default. A provider response of "!!!!" therefore exports 0 dimensions instead of the previous 4. Likewise, "AAAA" decodes to three bytes, which // 4 truncates to 0 despite not containing a complete float32.
Use strict decoding and check that the decoded length is divisible by four before counting dimensions. Add both cases to the fallback tests.
Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/openai.py
Line: 971
Comment:
**Malformed strings bypass the fallback**
`base64.b64decode` silently discards invalid characters by default. A provider response of `"!!!!"` therefore exports 0 dimensions instead of the previous 4. Likewise, `"AAAA"` decodes to three bytes, which `// 4` truncates to 0 despite not containing a complete float32.
Use strict decoding and check that the decoded length is divisible by four before counting dimensions. Add both cases to the fallback tests.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
Addressed in 5f2ecb6. Base64 decoding now uses strict validation, and decoded bytes are counted as float32 dimensions only when their length is divisible by four. Both !!!! and AAAA retain the previous string-length fallback of 4; the SDK response remains unchanged.
The two added cases fail on the previous PR head and pass with this fix. All 7 base64 cases and 68 affected OpenAI unit tests passed, along with Ruff, whole-tree formatting, and whole-package mypy. This follow-up was prepared and self-reviewed with Codex.
chrikrah
left a comment
There was a problem hiding this comment.
@as992949791 From my perspective this can be merged: at 5f2ecb6 a base64 embedding of 1536 floats exports dimensions: 1536, where base exported the string length, 8192.
# git archive 5f2ecb6 (base 043321f), Python 3.12.14, openai 3.42.1, langfuse.__file__ under the tree
$ python -m pytest tests/unit/test_openai.py -k embedding -q
9 passed, 36 deselected in 1.05s
# langfuse/openai.py from 043321f, tests kept
3 failed, 6 passed, 36 deselected in 0.89s
The CLA gate is the one red check on this head. Could you sign it via the CLAassistant link above so it can go to a maintainer?
Summary
Count decoded float32 elements when an OpenAI embedding response uses explicit
encoding_format="base64". Preserve the previous string-length fallback for malformed base64 or decoded bytes that do not form complete float32 elements. SDK responses pass through unchanged, and trace output still contains only dimensions and count.Type of change
What changed
httpx.MockTransportand the shared in-memory exporter, asserting both trace output and unchanged SDK-returned strings.Verification
All 7 base64 cases and all 68 affected OpenAI unit tests passed. Ruff, whole-tree format check, whole-package mypy and
git diff --checkpassed. No live provider or Langfuse server was used; the remote Python-version matrix was not run locally.Evidence
The original valid-base64 regressions report string lengths on the base implementation and float32 dimensions with the fix. The additional
!!!!andAAAAcases fail on the earlier PR head with 0 dimensions instead of the fallback 4; both now pass alongside valid vectors, malformed input, non-ASCII passthrough, and existing float-array coverage.Merge Danger
Door: two-way. Blast Radius: embedding trace output for explicit base64 responses. No public signatures or provider responses change.
Checklist
code_review.md(Codex review)..env.templateif needed. No documentation becomes stale from correcting this count.AI disclosure: The implementation, regression tests, and self-review were produced with OpenAI Codex. No human review is claimed.